﻿function statusFunc(result, ctx, req)
{
	if (req.completed)
	{
	    addRequest(req.postData);
	    addMethodToList('>>> CALLBACK - Size: ' + req.postData.length);
	    addResponse(result);
	    addMethodToList('>>> Render Size: ' + result.length);
	}
}

function addMethodToList(s)
{
	var oCtl = $(m_sNS + '_txtMethods');
	var ary = oCtl.value.split('\n');
	ary[ary.length-1] = ary.length + '. ' + s + '\n';
	oCtl.value = ary.join('\n');

}
	
function tabClick(evt, arg)
{
    var tab = arg.get_tab();
	var ts = tab.strip;//dnn.controls.controls[m_sNS + '_MyDNNTabStrip'];
	addMethodToList(tab.tabId + ' clicked');
	if (ts.needsLookup(tab.tabId))
	{
		if (ts.tabRenderMode == 2)//callback
		{
			//hardcoding results found in debug mode
			addMethodToList('SetupDefaults: tabAdvanced (Delay 1secs)');
			addMethodToList('RefreshColors');
			addMethodToList('BindTab: tabAdvanced (Delay 1secs)');			
		}
	}
}	

function initializeAtlasEvents()
{
    var postbackElement;
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest);
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoaded);
}

function beginRequest(sender, args) {
    var sHeaders = '';
    for (var s in args.get_request().get_headers())
    {
        sHeaders += s + '=' + args.get_request().get_headers()[s] + '\n';
    }
    var sBody = args.get_request().get_body();
    addRequest(sHeaders + sBody);
    addMethodToList('>>> POSTBACK - Size: ' + (sBody.length));
    
}
function pageLoaded(sender, args) {
    var sResponse = sender._response.get_responseData();
    var sHeaders = sender._response.getAllResponseHeaders();
    addResponse(sHeaders + sResponse);
    addMethodToList('>>> Render Size: ' + (sResponse.length));

    var updatedPanels = args.get_panelsUpdated();
    if (typeof(postbackElement) === "undefined") {
        return;
    } 
    else if (postbackElement.id.toLowerCase().indexOf('external') > -1) {
        for (i=0; i < updatedPanels.length; i++) {            
            panelUpdatedAnimation.animatePanel(updatedPanels[i]);
        }
    }

}

//if partial rendering would have run the script we send down, this would not be necessary
//this script allows us to catch the partial rendering complete event
function hookupAtlasPartialRenderingEvent() 
{ 
  var page = $object("_PageRequestManager"); 
  page.propertyChanged.add( atlasPartialRenderingComplete ); 
} 

function atlasPartialRenderingComplete(obj, args) 
{ 
  if (args.get_propertyName() && obj.get_inPostBack() == false) 
  { 
    //hardcoded based off of server-side debugging and use of fiddler
    addMethodToList('>>> POSTBACK - Size: ' + $(m_sNS + '_txtRequest').value.length);  
    addMethodToList('SetupDefaults: tabGeneral (Delay 1secs)');  
    addMethodToList('SetupDefaults: tabAdvanced (Delay 1secs)');  
    addMethodToList('RefreshColors');  
    addMethodToList('BindTab: tabAdvanced (Delay 1secs)');  
    addMethodToList('>>> Render Size: ' + $(m_sNS + '_txtResponse').value.length);    
    dnn.controls.initTabStrip($(m_sNS + 'MyDNNTabStrip'));  
  } 
} 

//tooltips describing why ratings were chosen.  This info is found in the script in order to keep the rendering mode's sizes on the same playing field.
//if we rendered the descriptions on the server-side or hardcoded in the markup the one with the longest descriptions would be unduely penalized.
var m_aRatings = new Array();
m_aRatings[0] = new ratingDesc('The initial processing for the TabRenderingMode of All is pretty bad as one would expect.  We are processing all tabs on the initial request.  You will see that this is a benefit if the user ends up clicking on each tab, but in cases where the user chooses not to open other tabs, this mode is far from optimal.', 
                               'The initial load for the TabRenderingMode of All is pretty bad as one would expect.  We are loading the html from all tabs on the initial request.  You will see that this is a benefit if the user ends up clicking on each tab, but in cases where the user chooses not to open other tabs, this mode is far from optimal.', 
                               'Obviously, the mode that rendered all of its contents to the client on the initial load will offer the best performance for the ensuing tab-clicks.  The real question is, was the slower page load worth the instant ability to view other tabs?', 
                               'Obviously, the mode that rendered all of its contents to the client on the initial load will offer the best performance for the ensuing tab-clicks.  The real question is, was the slower page load worth the instant ability to view other tabs?', 
                               'Obviously, the mode that rendered all of its contents to the client on the initial load will offer the best performance for the ensuing tab-clicks.  The real question is, was the slower page load worth the instant ability to view other tabs?');
m_aRatings[1] = new ratingDesc('The initial server processing here includes only what is displayed, therefore it is the best approach.',
                               'This approach is nearly the best for the initial loading.  This is due to the fact that only the displayed tab is rendered.  The reason it has a slightly larger render size is the script that ASP.NET injects for the postback handler.  The Callback method has no postback script, for the only controls on the page that would do a postback are the buttons, and they are submit buttons.   In a real application, this difference most likely will not be present, since there will usually be some other control that needs postback logic.', 
                               'A postback causes the entire client-side elements to be posted to the server, which is more information than we need in most cases.', 
                               'The request for the clicked tab’s information causes the entire page to be processed again.', 
                               'The contents of the entire page are marshaled back to the client. ');
m_aRatings[2] = new ratingDesc('The initial server processing here includes only what is displayed, therefore it is the best approach.',
                               'Like the postback rendering mode, this mode only renders the displayed tab, therefore the size of the rendering size is much smaller.  For reasons mentioned in the Postback description, its size is slightly smaller than the Postback.', 
                               'The only information posted to the server for this request is the tab which we are retrieving.  This allows for our payload to be really small.  The only mode that beats it is all, which doesn’t make the request at all.', 
                               'Only the page’s init method is called.  From there a call is made to the callback handler which simply processes the methods required to render the requested tab.', 
                               'Only the html for the new tab is pushed to the client.');
m_aRatings[3] = new ratingDesc('The initial server processing here includes only what is displayed, therefore it is the best approach.', 
                               'The rendering mode for the tabstrip for the partial rendering sample is using Postback.  As stated above, the postback rendering size is a little larger than callback.  This mode is slightly larger than postback, due to the markup necessary to enable partial rendering.', 
                               'Partial rendering uses postbacks, therefore it is the same as the Postback mode.  A postback causes the entire client-side elements to be posted to the server, which is more information than we need in most cases*.', 
                               'Similar to the Postback mode, the entire page is processed on the server in order to serve up the new tab.  The difference with the postback is that partial rendering short-circuits the final stages of the postback in order to send only the necessary information back.  One could argue that this additional processing to parse only the necessary info out of the response would be more processing than the postback, however, I have not investigated to determine if this is so.', 
                               'The html for the new tab, plus the updated viewstate, plus some additional markup outside the tab is sent down.');

function ratingDesc(initProc, initPayload, tabReq, tabProc, tabPayload)
{
  this.imgInitialRequestProcessing = initProc;
  this.imgInitalRequestPayload = initPayload;
  this.imgTabRequest = tabReq;
  this.imgTabRequestProcessing = tabProc;
  this.imgTabPayload = tabPayload;
}

function showRatingDesc(oImg, iMode)
{
  oImg.alt = m_aRatings[iMode][oImg.id.substring(m_sNS.length + 1)];
}

function addRequest(s)
{
    $(m_sNS + '_txtRequest').value += s + '\n';
}
function addResponse(s)
{
    $(m_sNS + '_txtResponse').value += s + '\n';
}